package com.liu.efk.zdcq.aop;

import com.liu.common.domain.ResultVo;
import lombok.val;
import lombok.var;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by shi860715@126.com
 * Date 2021/6/24 10:37
 * Description
 */
@Component
@Aspect
public class AopTest {


    @Autowired
    RestTemplate  restTemplate;


    String BaseUrl="http://218.244.147.216:7300/mock/605c36ce7d80851e3100458f/";

    ThreadLocal<HttpServletRequest> request = new ThreadLocal();

    @Pointcut("execution(public * com.liu.efk..UserController.*(..))")
    public void restMock() {
    }

    @Before("restMock()")
    public void doBefore(JoinPoint joinPoint){
        final var args = joinPoint.getArgs();
        for(Object arg :args){
            System.out.println(arg);
        }
    }



    @AfterReturning(pointcut = "restMock()",returning = "result")
    public void doAfterReturning(JoinPoint joinPoint,Object result)  throws Throwable{

        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
//        this.request.set(request);
        String url = request.getRequestURL().toString();
        String queryString = request.getQueryString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        System.out.println(url+uri+method+queryString);


       String mockresult = restTemplate.postForObject(BaseUrl+uri,null,String.class);

        if(result instanceof ResultVo){
            ((ResultVo) result).setData(mockresult);
        }



    }









}
